package com.itheima.spec.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.goods.pojo.Spec;
import com.itheima.spec.mapper.SpecDao;
import com.itheima.spec.service.SpecService;
import entity.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wang
 * @description
 * @date 2021/7/29 17:33
 */
@Service("specService")
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class SpecServiceImpl extends ServiceImpl<SpecDao, Spec> implements SpecService {
    @Override
    public List<Spec> findAll() {
        return this.list();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    public boolean add(Spec spec) {
        return this.save(spec);
    }

    @Override
    public PageResult findPage4Post(Integer page, Integer size,Spec spec) {
        //设置分页参数
        IPage<Spec> pageInfo = new Page<>(page, size);
        LambdaQueryWrapper<Spec> wrapper = Wrappers.lambdaQuery(Spec.class);
        if (spec.getName() != null && !"".equals(spec.getName().trim())) {
            wrapper.like(Spec::getName,spec.getName());
        }
        if (spec.getOptions() != null && !"".equals(spec.getOptions().trim())) {
            wrapper.like(Spec::getOptions,spec.getOptions());
        }
        if (spec.getSeq() != null) {
            wrapper.eq(Spec::getSeq,spec.getSeq());
        }
        wrapper.orderByAsc(Spec::getSeq).orderByAsc(Spec::getId);
        this.page(pageInfo,wrapper);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setRows(pageInfo.getRecords());
        return pageResult;
    }

    @Override
    public PageResult findPage4Get(Integer page, Integer size) {
        //设置分页参数
        IPage<Spec> pageInfo = new Page<>(page, size);
        LambdaQueryWrapper<Spec> wrapper = Wrappers.lambdaQuery(Spec.class);
        wrapper.orderByAsc(Spec::getSeq).orderByAsc(Spec::getId);
        this.page(pageInfo,wrapper);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setRows(pageInfo.getRecords());
        return pageResult;
    }

    @Override
    public Spec findById(Integer id) {
        return this.getById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    public boolean updateSpec(Integer id,Spec spec) {
        spec.setId(id);
        return this.updateById(spec);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    public boolean deleteById(Integer id) {
        return this.removeById(id);
    }
}
